local AudioFade = import(".AudioFade")

local AudioManager = {}

--local SAE_Instance = cc.SimpleAudioEngine:getInstance()
local AE_Instance = ccexp.AudioEngine
local _play2dFunc = ccexp.AudioEngine.play2d
local _setVolumeFunc = ccexp.AudioEngine.setVolume

AudioManager.TagSound = "sound"
AudioManager.TagMusic = "music"


AudioManager.MuteStates={}
AudioManager.MuteStates[AudioManager.TagSound]=false
AudioManager.MuteStates[AudioManager.TagMusic]=false

AudioManager.VolumeMax = {}
AudioManager.VolumeMax[AudioManager.TagSound]=1.0
AudioManager.VolumeMax[AudioManager.TagMusic]=1.0

local playingSounds={}

local FileUtils = cc.FileUtils:getInstance()
local _fullPathForFilenameFunc = cc.FileUtils.fullPathForFilename
local _isFileExistFunc = cc.FileUtils.isFileExist

------------------基本功能操作------------------------
function AudioManager.play(soundPath,isLoop,tag,callback)
	local audioId = nil
    if soundPath == nil or string.len(soundPath) <= 0 then
		return audioId
	end

	local tag_ = (tag or AudioManager.TagSound)
	local isMute = AudioManager.MuteStates[tag_]
	local volumeMax = AudioManager.VolumeMax[tag_]

	if isMute and tag_ == AudioManager.TagSound then
		if callback then
			callback()
		end
		return nil
	end

	audioId = _play2dFunc(AE_Instance,soundPath,isLoop)

	if audioId and  audioId >= 0 then
		playingSounds[audioId]=tag_
		AE_Instance:setFinishCallback(audioId,function(audioId,audioPath)
			if callback then
				callback()
			end
			playingSounds[audioId]=nil
		end)

		local volume = isMute and 0.0 or volumeMax
		_setVolumeFunc(AE_Instance,audioId,volume)
	end
	return audioId
end

-- 检查音乐是否在播放中，本地缓存的audioId有可能被stopAll全部清空
function AudioManager.isAudioInPlaying(audioId)
	if not audioId then return false end
	return playingSounds[audioId] ~= nil
end

function AudioManager.preload(soundPath)
	AE_Instance:preload(soundPath)
end

---
--Sound volume value (range from 0.0 to 1.0).
--
function AudioManager.setVolume(audioId,volume)
	if not audioId or audioId < 0 then 
		return nil 
	end 

	local tag = playingSounds[audioId]
	if not tag then
		return nil
	end
	
	local volumeMax = AudioManager.VolumeMax[tag]
	local isMute = AudioManager.MuteStates[tag]
	local v = math.min(volumeMax,volume or 0)
	if isMute then
		v = 0 
	end
	AE_Instance:setVolume(audioId,v)
end

function AudioManager.getVolume(audioId)
	if not audioId then 
		return AE_Instance:getVolume(audioId)
	end 
end

function AudioManager.setVolumeByTag(volume,tag)
	local isMute = AudioManager.MuteStates[tag]
	local volumeMax = AudioManager.VolumeMax[tag]
	local v = math.min(volumeMax,volume or 0)
	if isMute then
		v = 0 
	end

	for audioId,tag_ in pairs(playingSounds) do
		if tag == tag_ then
			AE_Instance:setVolume(audioId,v)
		end
	end
end

function AudioManager.pause(audioId)
	if audioId then 
		AE_Instance:pause(audioId)
	end 
end

function AudioManager.resume(audioId)
	if audioId then 
		AE_Instance:resume(audioId)
	end 
	
end

function AudioManager.stop(audioId)
	if audioId then 
		AE_Instance:stop(audioId)
		playingSounds[audioId]=nil
	end 
end

function AudioManager.uncache(filePath)
	AE_Instance:uncache(filePath)
end

function AudioManager.uncacheAll()
	AE_Instance:uncacheAll()
end

function AudioManager.getCurrentTime(audioId)
	if audioId then
		return AE_Instance:getCurrentTime(audioId)
	end
end

function AudioManager.setCurrentTime(audioId,v)
	if audioId then
		return AE_Instance:setCurrentTime(audioId,v)
	end
end


------------------音乐音效全体操作------------------------
function AudioManager.pauseAll()
	AE_Instance:pauseAll()
end

function AudioManager.resumeAll()
	AE_Instance:resumeAll()
end

function AudioManager.fadeOutAll(fadeTime)
	for audioId,tag_ in pairs(playingSounds) do
		AudioManager.fadeOut(audioId,fadeTime)
	end
end

function AudioManager.fadeInAll(fadeTime)
	for audioId,tag_ in pairs(playingSounds) do
		AudioManager.fadeIn(audioId,fadeTime)
	end
end

function AudioManager.stopAll()
	AE_Instance:stopAll()
	playingSounds={}
end

function AudioManager.pauseByTag(tag)
	for audioId,tag_ in pairs(playingSounds) do
		if tag == tag_ then
			AudioManager.pause(audioId)
		end
	end
end

function AudioManager.resumeByTag(tag)
	for audioId,tag_ in pairs(playingSounds) do
		if tag == tag_ then
			AudioManager.resume(audioId)
		end
	end
end

function AudioManager.stopByTag(tag)
	for audioId,tag_ in pairs(playingSounds) do
		if tag == tag_ then
			AudioManager.stop(audioId)
			playingSounds[audioId]=nil
		end
	end
end

function AudioManager.setMute(tag,b)
	local isMute = AudioManager.MuteStates[tag]
	if isMute==b then
		return 
	end
	AudioManager.MuteStates[tag]=b
	if b then
		AudioManager.setVolumeByTag(0,tag)
	else
		AudioManager.setVolumeByTag(1,tag)
	end
end


function AudioManager.setAllMute(b)
	AudioManager.setSoundMute(b)
	AudioManager.setMusicMute(b)
end

------------------背景音乐操作------------------------

function AudioManager.setSoundMute(b)
	AudioManager.setMute(AudioManager.TagSound,b)
end

function AudioManager.setMusicMute(b)
	AudioManager.setMute(AudioManager.TagMusic,b)
end

function AudioManager.setSoundVolumeMax(v)
	AudioManager.VolumeMax[AudioManager.TagSound] = v
	AudioManager.setVolumeByTag(v,AudioManager.TagSound)
end

function AudioManager.setMusicVolumeMax(v)
	AudioManager.VolumeMax[AudioManager.TagMusic] = v
	AudioManager.setVolumeByTag(v,AudioManager.TagMusic)
end

function AudioManager.getSoundVolumeMax(v)
	return AudioManager.VolumeMax[AudioManager.TagSound]
end

function AudioManager.getMusicVolumeMax(v)
	return AudioManager.VolumeMax[AudioManager.TagMusic]
end

function AudioManager.isMuteSound()
	local isMute = AudioManager.MuteStates[AudioManager.TagSound]
	return isMute
end

function AudioManager.isMuteMusic()
	local isMute = AudioManager.MuteStates[AudioManager.TagMusic]
	return isMute
end

function AudioManager.pauseMusic()
	AudioManager.pauseByTag(AudioManager.TagMusic)
end

function AudioManager.resumeMusic()
	AudioManager.resumeByTag(AudioManager.TagMusic)
end


function AudioManager.fadeIn(audioId,duration)
	if audioId then
		local fadeObj = AudioFade.new(audioId,duration,0,1)
		local scene = cc.Director:getInstance():getRunningScene()
		scene:addChild(fadeObj)
	end
end

function AudioManager.fadeOut(audioId,duration)
	if audioId then
		local fadeObj = AudioFade.new(audioId,duration,1,0,function(audioId)
			AudioManager.stop(audioId)
		end)
		local scene = cc.Director:getInstance():getRunningScene()
		scene:addChild(fadeObj)
	end
end

------------------快捷操作------------------------
function AudioManager.playById(id,isLoop,tag,callback)
	local soundPath = GD:getSoundEffectPathById(id)
    if soundPath then
        return AudioManager.play(soundPath,isLoop,tag,callback)
    else
    	print("AudioManager.playById() can't find id = " .. id)
    end
end

function AudioManager.playSoundById(id,callback)
	AudioManager.playById(id, false, nil, callback)
end

function AudioManager.playClickItemSound()
	AudioManager.playById(3001, false)
end

--装备更换成功音效
function AudioManager.playChangeEquipSound()
	AudioManager.playById(2015, false)
end

return AudioManager